package com.nsu.cl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nsu.cl.pojo.Subject;
import com.nsu.cl.service.SubjectService;
import com.nsu.cl.mapper.SubjectMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
* @author 52618
* @description 针对表【subject(科目表)】的数据库操作Service实现
* @createDate 2023-02-16 14:21:02
*/
@Service
@Transactional
public class SubjectServiceImpl extends ServiceImpl<SubjectMapper, Subject>
    implements SubjectService{

    @Override
    public List<Subject> getSubjects() {
        return baseMapper.selectList(null);
    }

    @Override
    public IPage<Subject> getSubjectByOpr(Page<Subject> pageParam, String name) {

        QueryWrapper<Subject> queryWrapper = new QueryWrapper<>();

        if (!StringUtils.isEmpty(name)) {
            queryWrapper.like("name",name);
        }

        queryWrapper.orderByAsc("id");

        Page<Subject> page = baseMapper.selectPage(pageParam, queryWrapper);

        return page;
    }

    @Override
    public Subject getByName(Subject subject) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if(!StringUtils.isEmpty(subject.getName())) {
            queryWrapper.eq("name",subject.getName());
        }
        Subject sub = baseMapper.selectOne(queryWrapper);
        return sub;
    }
}




